/***************************************************
> Copyright (C) 2024 ==ASD== All rights reserved.
> File Name: userIntegral.c
> Author: asd
> Mail:3319941451@qq.com 
> Created Time: 2024年12月03日 星期二 20时09分30秒
***************************************************/
 #include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
char name[100];
int integral;
int quantity;
} Product;

// 购物清单链表节点
typedef struct Node 
{
Product product;
struct Node* next;
} Node;

Node* head = NULL; // 头节点指针
 
// 添加商品到购物清单
void addProduct() 
{
    Node* newNode = (Node*)malloc(sizeof(Node));
	memset(newNode,0,sizeof(Node));
    printf("请输入商品名称：");
    scanf("%s", newNode->product.name);
    //printf("请输入商品积分：");
    //scanf("%d", &newNode->product.integral);
    printf("请输入商品数量：");
    scanf("%d", &newNode->product.quantity);
    newNode->next = NULL;
 
    if (head == NULL) 
	{
        head = newNode;
    } else {
        Node* current = head;//确定头节点
        while (current->next != NULL) 
		{
            current = current->next;
        }
        current->next = newNode;//创建链表
    }
 
    printf("商品已添加到购物清单。\n");
}
 
// 计算购物清单总积分
int* calculateTotalIntegral() 
{
    int totalIntegral = 0;
    Node* current = head;
    while (current != NULL) 
	{
		//得到总积分
        totalIntegral += current->product.integral * current->product.quantity;
        current = current->next;
    }//循环读取链表每一个位置
    return totalIntegral;
}
 
// 显示购物清单
void showShoppingList() 
{
    Node* current = head;
    while (current != NULL) 
	{
        printf("商品名称：%s，积分：%d，数量：%d\n", current->product.name, current->product.integral,current->product.quantity);
        current = current->next;
    }//显示链表里面的数据
}
 
// 保存购物清单到文件
void saveShoppingListToFile() 
{
	//指向 FILE 结构体的指针，用于管理文件流
    FILE* file = fopen("shopping_list.txt", "w");//用来打开文件流的，它返回一个指向 FILE 结构体的指针
    //"w" 是打开文件的模式，其中 "w" 表示以写入模式打开文件。如果文件不存在，则创建一个新文件；如果文件已存在，则将其内容清空。
    if (file == NULL) 
	{
        printf("无法打开文件。\n");
        return;
    }
 
    Node* current = head;
    while (current != NULL) 
	{
        fprintf(file, "%s %d %d\n", current->product.name, current->product.integral, current->product.quantity);
       //用于将格式化的数据写入文件
        current = current->next;
    }
 
    fclose(file);//用于关闭购物清单文件流 file，以确保文件被正确地写入磁盘并释放相关资源
    printf("购物清单已保存到文件。\n");
}
 
// 从文件加载购物清单
void loadShoppingListFromFile() 
{
    FILE* file = fopen("shopping_list.txt", "r");//“r ”读模式
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }
 
    char name[100];
    int integral;
    int quantity;
 
    while (fscanf(file, "%s %d %d", name, &integral, &quantity) != EOF) 
	{
        Node* newNode = (Node*)malloc(sizeof(Node));
        strcpy(newNode->product.name, name);
        newNode->product.integral = integral;
        newNode->product.quantity = quantity;
        newNode->next = NULL;
 
        if (head == NULL) 
		{
            head = newNode;
        } else 
		{
            Node* current = head;
            while (current->next != NULL) 
			{
                current = current->next;
            }
            current->next = newNode;
        }
    }
 
    fclose(file);
    printf("购物清单已从文件加载。\n");
}
 
// 释放购物清单内存
void freeShoppingList() 
{
    Node* current = head;
    while (current != NULL) 
	{
        Node* next = current->next;
        free(current);//清除当前节点
        current = next;//当前节点向下移动
    }
    head = NULL;
}
void shopping()
{
    	loadShoppingListFromFile();
	displayMenu();
 }
int displayMenu()
{
     	int choice;
	int totalIntegral = 0;
	while (1) 
   	{
        	printf("\n超市购物计价系统\n");
       	 	printf("1. 添加商品到购物清单\n");
        	printf("2. 计算购物清单总价\n");
        	printf("3. 显示购物清单\n");
        	printf("4. 保存购物清单到文件\n");
        	printf("5. 退出系统\n");
       	 	printf("请选择：");
        	scanf("%d", &choice);
 
        switch (choice) 
	{
            case 1:
                addProduct();
                break;
            case 2: {
                totalIntegral = calculateTotalIntegral();
                printf("购物清单所需积分为：%d\n", totalIntegral);
                break;
            }
            case 3:
                showShoppingList();
                break;
            case 4:
                saveShoppingListToFile();
                break;
            case 5:
                freeShoppingList();
                exit(0);
            default:
                printf("无效的选项。\n");
        }
    }
 
    return totalIntegral;
}
